package com.zklh.leetcode.interviewTop150;

import java.util.HashMap;
import java.util.Map;

/**
 * 赎金信
 * author: tongnan
 * date: 2024/7/31
 */
public class Solution383 {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> ransomNoteCharToTime = new HashMap<>();
        Map<Character, Integer> magazineCharToTime = new HashMap<>();
        full(ransomNote, ransomNoteCharToTime);
        full(magazine, magazineCharToTime);
        for (Map.Entry<Character, Integer> entry : ransomNoteCharToTime.entrySet()) {
            if (magazineCharToTime.getOrDefault(entry.getKey(), 0) < entry.getValue()) {
                return false;
            }
        }
        return true;
    }

    private void full(String target, Map<Character, Integer> charToTime) {
        target.chars().forEach(value -> charToTime.put((char)value, charToTime.getOrDefault((char)value, 0) + 1));
    }

    public static void main(String[] args) {
        Solution383 solution383 = new Solution383();
        System.out.println(solution383.canConstruct("aa", "ab"));
    }
}
